package cyx.web;

import cyx.web.service.GitPath;
import cyx.web.service.ILocalPath;
import cyx.web.service.ProjectService;
import cyx.web.service.StringPath;
import cyx.web.utils.ZipTool;
import org.apache.commons.cli.*;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        Options options = new Options();
        options.addRequiredOption("t", "type", true, "获取地址的方式，git（从git的提交记录获取）file（从文件获取）");
        options.addRequiredOption("path", "path", true, "-t为git时（.git的路径）为file时（存储url文件路径）");
        options.addRequiredOption("froot", "from-root", true, "本地要被复制的主目录");
        options.addRequiredOption("troot", "to-root", true, "副本存放的路径");
        options.addOption("findex", "from-index", true, "从哪里开始：-t为git时（距离现在最近的第几次提交）");
        options.addOption("tindex", "to-index", true, "到哪里结束：-t为git时（距离现在最近的第几次提交）");
        CommandLineParser parser = new DefaultParser();
        ILocalPath localPath = null;
        String path = "";
        try {
            CommandLine cmd = parser.parse(options, args);
            String froot = "";
            String troot = "";
            if (cmd.hasOption("path")) {
                path = cmd.getOptionValue("path");
            }
            if (cmd.hasOption("froot")) {
                froot = cmd.getOptionValue("froot");
            }
            if (cmd.hasOption("troot")) {
                troot = cmd.getOptionValue("troot");
            }
            if (cmd.hasOption("t")) {
                String tParame = cmd.getOptionValue("t");
                switch (tParame) {
                    case "file":
                        localPath = new StringPath(path);
                        break;
                    case "git":
                    default:
                        int findex = 0;
                        int toindex = 1;
                        if (cmd.hasOption("findex")) {
                            try {
                                findex = Integer.parseInt(cmd.getOptionValue("findex"));
                            } catch (NumberFormatException exception) {
                                findex = 0;
                            }
                        }
                        if (cmd.hasOption("tindex")) {
                            try {
                                toindex = Integer.parseInt(cmd.getOptionValue("tindex"));
                            } catch (NumberFormatException exception) {
                                toindex = 1;
                            }
                        }
                        //"D:/Project/main_php/.git"
                        localPath = new GitPath(path, findex, toindex);
                }
            }
            ProjectService project = new ProjectService(localPath);
            //"D:/Project/main_php/", "D:/Project/git_plugin_webupd/Project/main_php/"
            project.copyFile(froot, troot);
            File f = new File(troot);
            new ZipTool(troot+"../" , "main_php.zip").zipFiles(f);
          //  FileTool.move(troot + "main_php.zip", troot + "../" ,"main_php.zip");
        } catch (MissingOptionException e) {
            List<String> exceptionList = e.getMissingOptions();
            StringBuffer sb = new StringBuffer("");
            for (String str : exceptionList) {
                sb.append("缺少必要参数:").append(str).append("\n");
            }
            System.out.println(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}
